package midend.IR_Instruction;

import midend.IR_Type.IR_Type;
import midend.IR_Value.IR_BaseBlock;
import midend.IR_Value.IR_FuncBlock;
import midend.IR_Value.IR_Local_Value;
import midend.IR_Value.IR_Value;

import java.util.ArrayList;
import java.util.List;

public class Store_Instruction extends Instruction{
    public Store_Instruction(List<IR_Value> op, IR_BaseBlock bb) {
        super(op, bb);
        addValue(get_op_index(1));
        addValue(get_op_index(2));
    }

    public IR_Value getPointer(){
        return get_op_index(2);
    }

    public void updateop(int index, IR_Value newValue) {
        if(index >= 0 && index <= 1){
            set_Op(index + 1, newValue);
        }
    }
    @Override
    public String toString() {
        // [tmp  值  存储的地址]
        return "store " + get_op().get(1).get_Type().get_TypeName() + " " +
                get_op().get(1).get_ValueName() + ", " +
                get_op().get(2).get_Type().get_TypeName() + " " + get_op().get(2).get_ValueName();
    }

    @Override
    public Store_Instruction clone(IR_BaseBlock block) {
        String copy_name = ((IR_Local_Value) get_op().get(0)).get_Local_Identity();
        IR_Type copy_type = get_op().get(0).get_Type();
        IR_Value res = new IR_Local_Value(copy_name, copy_type);
        ArrayList<IR_Value> copy_ops = new ArrayList<>();
        copy_ops.add(res);
        for (int i = 1 ; i< get_op().size() ; i++) {
            IR_Value copy_op  = get_op().get(i);
            if(IR_FuncBlock.cloneMap_Value.containsKey(copy_op)) {
                copy_op = IR_FuncBlock.cloneMap_Value.get(copy_op);
            }
            copy_ops.add(copy_op);
        }
        Store_Instruction copy_ins = new Store_Instruction(copy_ops, block);
        return copy_ins;
    }
}
